﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataBaseLayer.Abstractions;
using DocumentManager.Abstractions.Factories;
using DocumentManager.Abstractions.Interfaces.Helpers;
using DocumentManager.Common;

namespace DocumentManager.Forms
{
	public partial class frmLogOrder : Form
	{
		#region Variables

		private readonly IWordDocumentAdapter _wordDocumentAdapter;
		private readonly ServiceBaseFactory _serviceFactory;
		private readonly IApplicationConfiguration _applicationConfiguration;
		private readonly FormBaseFactory _formFactory;

		#endregion

		#region Constructors

		public frmLogOrder(
			ServiceBaseFactory serviceFactory,
			IApplicationConfiguration applicationConfiguration,
			IWordDocumentAdapter wordDocumentAdapter,
			FormBaseFactory formFactory)
		{
			if (serviceFactory == null)
				throw new ArgumentNullException(string.Format("service Factory"));
			_serviceFactory = serviceFactory;

			if (applicationConfiguration == null)
				throw new ArgumentNullException(string.Format("application Configuration"));
			_applicationConfiguration = applicationConfiguration;

			if (wordDocumentAdapter == null)
				throw new ArgumentNullException(string.Format("word Document Adapter"));
			_wordDocumentAdapter = wordDocumentAdapter;

			if (formFactory == null)
				throw new ArgumentNullException(string.Format("form Factory"));
			_formFactory = formFactory;

			InitializeComponent();
		}

		#endregion

		#region Form Actions

		private void btnGenerateDocument_Click(object sender, EventArgs e)
		{
			this.Enabled = false;

			try
			{
				var templateFile = _applicationConfiguration.LogOrdersTemplateFilePath;

				var newTempFilePath = _wordDocumentAdapter
					.CreateTempDocumentByTemplate(
						templateFile,
						_applicationConfiguration.TempOrderDocumentName);
				if (string.IsNullOrWhiteSpace(newTempFilePath))
					return;

				var data = CreateOrderMergedData(dtpStartDate.Value, dtpEndDate.Value);
				_wordDocumentAdapter.MergeDocumentTableData(newTempFilePath, data);

				var documentViewerForm = _formFactory.CreateDocumentViewerForm(newTempFilePath);
				if (documentViewerForm != null)
					documentViewerForm.ShowDialog();
			}
			catch (Exception ex)
			{
				MessageBox.Show(string.Format("При попытки печати документа возникла ошибка!{0}{1}", Environment.NewLine, ex.Message));
			}

			this.Enabled = true;
		}

		#endregion

		#region Internal Iplementation

		private IEnumerable<IDictionary<string, string>> CreateOrderMergedData(DateTime startDate, DateTime endDate)
		{
			var service = _serviceFactory.CreateLogOrdersService();
			if (service == null)
				throw new ArgumentNullException(string.Format("Log Orders Service"));

			var logOrders = service.GetLogOrdersByDateRange(startDate, endDate);
			if (logOrders != null && logOrders.Any())
			{
				return logOrders
					.Select(logOrder => new Dictionary<string, string>
					{
						{Constants.NUMBER_FIELD_NAME, logOrder.Number ?? string.Empty},
						{Constants.DATE_FIELD_NAME, logOrder.Date.ToShortDateString()},
						{Constants.DESCRIPTION_FIELD_NAME, logOrder.Description ?? string.Empty},
						{Constants.PERFORMER_FIELD_NAME, logOrder.Performer ?? string.Empty}
					})
					.Cast<IDictionary<string, string>>()
					.ToList();
			}

			return null;
		}

		#endregion
	}
}
